<html>
<head><meta charset="utf-8"><title>coding style: apparent (if not explicit) drop sites · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html">coding style: apparent (if not explicit) drop sites</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="204246559"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/204246559" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#204246559">(Jul 17 2020 at 19:09)</a>:</h4>
<p>I got into a little debate recently during a PR review, and I figured I might ask around for opinions here</p>



<a name="204246803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/204246803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#204246803">(Jul 17 2020 at 19:11)</a>:</h4>
<p>The debate is/was <a href="https://github.com/rust-lang/rust/pull/72121#discussion_r451006771">here</a> (cc'ing <span class="user-mention" data-user-id="125294">@Aaron Hill</span> ); the heart of it is as follows:</p>



<a name="204246868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/204246868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#204246868">(Jul 17 2020 at 19:11)</a>:</h4>
<p>if you have a bit of code where the temporary rvalue lifetimes are semantically important (e.g. when a lock will get unlocked by its destructor)</p>



<a name="204247033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/204247033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#204247033">(Jul 17 2020 at 19:13)</a>:</h4>
<p>the question is: Is it useful to surround the important boundary of the temporary lifetime (i.e. the temporary value's dynamic extent) with braces <code>{</code> ... <code>}</code></p>



<a name="204247077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/204247077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#204247077">(Jul 17 2020 at 19:14)</a>:</h4>
<p>even if the temporary r-value lifetime rules we have place imply that those added braces have no effect?</p>



<a name="204278297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/204278297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#204278297">(Jul 18 2020 at 02:03)</a>:</h4>
<p>I generally prefer just <code>drop(lock)</code> or similar, to avoid rightward drift.</p>



<a name="204278392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/204278392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#204278392">(Jul 18 2020 at 02:05)</a>:</h4>
<p>but yes I do tend to annotate things like locks where it matters, just so that its obvious on a quick read what's happening, even to people who don’t necessarily know the rules by heart.</p>



<a name="204278452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/204278452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#204278452">(Jul 18 2020 at 02:06)</a>:</h4>
<p>that said, there's no way that I can tell, to see the code being referenced by that comment anymore</p>



<a name="204278462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/204278462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#204278462">(Jul 18 2020 at 02:07)</a>:</h4>
<p>but I suspect it was something like <code>{foo.lock().banana()}.peach()</code>?</p>



<a name="205291239"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/205291239" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#205291239">(Jul 28 2020 at 21:10)</a>:</h4>
<p>I think that might be actively misleading</p>



<a name="205291245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/205291245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#205291245">(Jul 28 2020 at 21:10)</a>:</h4>
<p>at least unless we adjust the lifetime of temporaries in the tail expression of a block...</p>



<a name="205291261"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/205291261" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#205291261">(Jul 28 2020 at 21:10)</a>:</h4>
<p>e.g., in nagisa's example, the lock will not be droppe until the end of the enclosing statement, despite the <code>{}</code></p>



<a name="205291892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/205291892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#205291892">(Jul 28 2020 at 21:16)</a>:</h4>
<p>I tend to avoid explicit calls to <code>mem::drop</code>, as they introduce separate code-paths for happy path vs early-return/unwinding.</p>



<a name="205292005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/205292005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#205292005">(Jul 28 2020 at 21:18)</a>:</h4>
<p>the early return is what makes the separate code path, not the call to drop</p>



<a name="205292023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/205292023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#205292023">(Jul 28 2020 at 21:18)</a>:</h4>
<p>well it's the combination of the two :)</p>



<a name="205333481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/205333481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#205333481">(Jul 29 2020 at 09:16)</a>:</h4>
<p>Yeah, to clarify, the code where this bit me hard was something like:</p>
<div class="codehilite"><pre><span></span><code>let sender = ...;
let worker = ....;

drop(sender); // make sure that worker can shut down
drop(worker); // join worker, can join now because the sender is closed
</code></pre></div>


<p>This worked well until one day rust-analyzer hanged after a panic;</p>
<p>Changing that to</p>
<div class="codehilite"><pre><span></span><code>let worker;
let sender = ...;
worker = ....;
</code></pre></div>


<p>fixed the issue In an automatic way.</p>



<a name="205370295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/205370295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#205370295">(Jul 29 2020 at 15:14)</a>:</h4>
<p>interesting example</p>



<a name="205421774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/205421774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#205421774">(Jul 29 2020 at 22:34)</a>:</h4>
<p>So this changed the drop order compared to when explicit drops were used?</p>



<a name="205455085"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/coding%20style%3A%20apparent%20%28if%20not%20explicit%29%20drop%20sites/near/205455085" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/coding.20style.3A.20apparent.20(if.20not.20explicit).20drop.20sites.html#205455085">(Jul 30 2020 at 08:51)</a>:</h4>
<p>Uhu. Or, rather, explicit drop changed drop order for happy path, and only for happy path</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>